-- 2012-05-17 EM

alter procedure sp_get_phone_by_identificator_codes
	@code_a varchar(200),
	@code_b varchar(200),
	@uid int output,
	@phone varchar(20) output
as
begin
	set nocount on;
	
	declare @now datetime set @now = getdate();
	select top 1 @phone = p.phone , @uid=p.uid
	  from dbdata.dbo.phones p, dbdata.dbo.identificators i, dbdata.dbo.identificator_sets s
	 where i.code_b = @code_b
		and s.code_a=@code_a
	   and i.enabled = 1 
	   and s.id = i.set_id
	   and @now between s.dt_beg and isnull(s.dt_end, @now + 1) 
	   and p.uid = i.uid
	   and p.enabled = 1
	   and p.as_default = 1
end;
go

grant execute on sp_get_phone_by_identificator_codes to Executer;

/*
DECLARE	@return_value int,
		@uid int,
		@phone varchar(20)

EXEC	@return_value = [dbo].[sp_get_phone_by_identificator_codes]
		@code_a = N'login',
		@code_b = N'irinasot',
		@uid = @uid OUTPUT,
		@phone = @phone OUTPUT

SELECT	@uid as N'@uid',
		@phone as N'@phone'

SELECT	'Return Value' = @return_value
*/